package com.lxq.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.lxq.common.DaoFactory;
import com.lxq.dao.UserDao;
import com.lxq.entity.User;
import com.lxq.web.servlet.common.BaseServlet;

@WebServlet("/app/user")
public class FindUserServlet extends BaseServlet{
	private static final long serialVersionUID = -6513245338712952138L;
	UserDao userDao = DaoFactory.getInstance("userDao", UserDao.class);
	
	@Override
	public void doFindphone(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String telephone = request.getParameter("telephone");
		
		User user=userDao.findByPhone(telephone);
		System.out.println(user==null);
		JSONObject json = new JSONObject();
		
		try {
			if(user==null){
				json.put("success", false);

				json.put("reason", "未找到绑定该手机号的用户");
			}else{
				json.put("success", true);
				JSONObject userJSON = new JSONObject(user);
				json.put("user", userJSON);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(json.toString());
		response.getWriter().flush();
	}

}
